<template>
  <div class="subject">
    <span>{{ numOne }}</span>
    <span>+</span>
    <span>{{ numTwo }}</span>
    <span>=</span>
    <input type="number" v-model.trim.number="res" />
    <button @click="submit">提交</button>
  </div>
</template>

<script>
export default {
  name: "SubjectItem",
  data() {
    return {
      numOne: 0,
      numTwo: 0,
      res: "",
    };
  },
  methods: {
    randNum() {
      return Math.floor(Math.random() * 10) + 1;
    },
    submit() {
      if (!this.res) {
        this.$emit("changeRes", "未完成");
      } else if (this.numOne + this.numTwo === this.res) {
        this.$emit("changeRes", "回答正确");
      } else {
        this.$emit("changeRes", "回答错误");
      }
    },
  },
  created() {
    this.numOne = this.randNum();
    this.numTwo = this.randNum();
  },
};
</script>

<style scoped>
.subject {
  margin: 5px;
  padding: 5px;
  font-size: 20px;
}
.subject span {
  display: inline-block;
  text-align: center;
  width: 20px;
}
.subject input {
  width: 50px;
  height: 20px;
}
</style>
